package com.farsunset.cim.client.android;

import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.farsunset.cim.nio.mutual.Message;
import com.farsunset.cim.nio.mutual.ReplyBody;
import com.farsunset.cim.nio.mutual.SentBody;
/**
 *  消息入口，所有消息都会经过这里
 * @author 3979434
 *
 */
public  abstract  class CIMEnventListenerReceiver extends BroadcastReceiver implements OnCIMMessageListener {
 

	public Context context;
	@Override
	public void onReceive(Context ctx, Intent it) {

		  context = ctx;
		  
		  if(it.getAction().equals(CIMConnectorManager.ACTION_NETWORK_CHANGED))
          {
			  ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService("connectivity");
	          android.net.NetworkInfo info = connectivityManager.getActiveNetworkInfo();
	        
	          onNetworkChanged(info);
          }
		
          if(it.getAction().equals(CIMConnectorManager.ACTION_CONNECTION_CLOSED))
          {
        	  onConnectionClosed();
          }
          
          if(it.getAction().equals(CIMConnectorManager.ACTION_CONNECTION_FAILED))
          {
        	  onConnectionFailed((Exception) it.getSerializableExtra("exception"));
          }
          
          if(it.getAction().equals(CIMConnectorManager.ACTION_CONNECTION_SUCCESS))
          {
        	  onConnectionSucceed();
          }
          
          if(it.getAction().equals(CIMConnectorManager.ACTION_MESSAGE_RECEIVED))
          {
        	  onMessageReceived((Message)it.getSerializableExtra("message"));
          }
          
          if(it.getAction().equals(CIMConnectorManager.ACTION_REPLY_RECEIVED))
          {
        	  onReplyReceived((ReplyBody)it.getSerializableExtra("replyBody"));
          }
          
          if(it.getAction().equals(CIMConnectorManager.ACTION_SENT_FAILED))
          {
        	  onSentFailed((Exception) it.getSerializableExtra("exception"),
        			  (SentBody)it.getSerializableExtra("sentBody"));
          }
          
          if(it.getAction().equals(CIMConnectorManager.ACTION_SENT_SUCCESS))
          {
        	  onSentSucceed((SentBody)it.getSerializableExtra("sentBody"));
          }

          if(it.getAction().equals(CIMConnectorManager.ACTION_UNCAUGHT_EXCEPTION))
          {
        	  onUncaughtException((Exception)it.getSerializableExtra("exception"));
          }
	}

	protected boolean isInBackground(Context context) {
		List<RunningTaskInfo> tasksInfo = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getRunningTasks(1);
		if (tasksInfo.size() > 0) {

			if (context.getPackageName().equals(
					tasksInfo.get(0).topActivity.getPackageName())) {

				return false;
			}
		}
		return true;
	}

	@Override
	public abstract void onConnectionClosed();

	@Override
	public abstract void onConnectionFailed(Exception e);

	@Override
	public abstract void onConnectionSucceed();

	@Override
	public void onUncaughtException(Throwable arg0) {}

	@Override
	public abstract void onMessageReceived(com.farsunset.cim.nio.mutual.Message message);

	@Override
	public abstract void onNetworkChanged(NetworkInfo info) ;

	@Override
	public abstract void onReplyReceived(ReplyBody body);

	@Override
	public abstract void onSentFailed(Exception e, SentBody body);

	@Override
	public abstract void onSentSucceed(SentBody body);
}
